<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <!-- <i class="el-icon-search"></i>
        <span>筛选搜索</span> -->
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="资源名称：">
            <el-input v-model="listQuery.nameKeyword" class="input-width" placeholder="资源名称" clearable></el-input>
          </el-form-item>
          <el-form-item label="资源路径：">
            <el-input v-model="listQuery.urlKeyword" class="input-width" placeholder="资源路径" clearable></el-input>
          </el-form-item>
          <el-form-item label="资源分类：">
            <el-select v-model="listQuery.resourceType" placeholder="全部" clearable class="input-width">
              <el-option v-for="item in categoryOptions"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="菜单：">
            <el-select v-model="listQuery.menuId" placeholder="全部" clearable class="input-width">
              <el-option v-for="item in menuOptions"
                         :key="item.id"
                         :label="item.title"
                         :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div style="width: 200px;">
        <el-button
          style="float:right"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <!-- <i class="el-icon-tickets"></i>
      <span>数据列表</span> -->
      <el-button size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加</el-button>
      <!-- <el-button size="mini" class="btn-add" @click="handleShowCategory()">资源分类</el-button> -->
    </el-card>
    <div class="table-container">
      <el-table ref="resourceTable"
                :data="list"
                style="width: 100%;"
                v-loading="listLoading" border>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="资源名称" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="资源路径" align="center" width="680">
          <template slot-scope="scope">{{scope.row.url}}</template>
        </el-table-column>
        <el-table-column label="描述" align="center">
          <template slot-scope="scope">{{scope.row.description}}</template>
        </el-table-column>
        <el-table-column label="添加时间" width="160" align="center">
          <template slot-scope="scope">{{scope.row.createTime | formatDateTime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="140" align="center">
          <template slot-scope="scope">
            <el-button size="mini"
                       type="text"
                       @click="handleUpdate(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button size="mini"
                       type="text"
                       @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit?'编辑资源':'添加资源'"
      :visible.sync="dialogVisible"
      width="55%">
      <el-form :model="resource"
               ref="resourceForm"
               label-width="150px" size="small">
        <el-form-item label="资源名称：">
          <el-input v-model="resource.name" style="width: 450px"></el-input>
        </el-form-item>
        <el-form-item label="资源路径：">
          <el-input v-model="resource.url" style="width: 450px"></el-input>
        </el-form-item>
        <el-form-item label="前段按钮路径：">
          <el-input v-model="resource.component" style="width: 450px"></el-input>
        </el-form-item>
        <el-form-item label="资源分类：">
          <el-select v-model="resource.resourceType" placeholder="全部" clearable style="width: 450px">
            <el-option v-for="item in categoryOptions"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分配菜单：">
          <el-select v-model="resource.menuId" placeholder="全部" clearable style="width: 450px">
            <el-option v-for="item in menuAddOptions"
                       :key="item.id"
                       :label="item.title"
                       :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否必要：">
          <el-radio-group v-model="necessaryYn">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="分配菜单">
          <el-tree
            :data="treeList"
            show-checkbox
            node-key="id"
            ref="tree"
            highlight-current
            :default-checked-keys="checkedTree"
            :props="defaultProps">
          </el-tree>
        </el-form-item> -->
        <el-form-item label="描述：">
          <el-input v-model="resource.description"
                    type="textarea"
                    :rows="5"
                    style="width: 250px"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {fetchList,createResource,updateResource,deleteResource} from '@/api/resource';
  import { getAllMenu , getResourceTree, getDetailTree, getAddMenu } from '@/api/app';
  import {formatDate} from '@/utils/date';

  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    nameKeyword: null,
    urlKeyword: null,
    resourceType: null,
    menuId: null
  };
  const defaultResource = {
    id: null,
    createTime: null,
    name: null,
    url: null,
    description: null,
    menuId: null,
    component: null,
    necessaryYn: null,
    resourceType: null
  };
  export default {
    name: 'resourceList',
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        listLoading: false,
        dialogVisible: false,
        resource: Object.assign({}, defaultResource),
        necessaryYn: null,
        isEdit: false,
        categoryOptions:[
          {
            label: '接口',
            value: 1
          },
          {
            label: '按钮',
            value: 2
          }
        ],
        menuAddOptions: [],
        defaultCategoryId: null,
        menuOptions: [],
        treeList: [],
        defaultProps: {
          children: 'children',
          label: 'title'
        },
        checkedTree: [],
      }
    },
    created() {
      this.getList();
      getAllMenu().then(res => {
        // console.log(res);
        this.menuOptions = res.data;
      });
      getAddMenu().then(res => {
        // console.log(res);
        this.menuAddOptions = res.data;
      });
      this.getTree();
    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === '') {
          return '-';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      }
    },
    methods: {
      getTree() {
        getResourceTree().then(res => {
          this.treeList = this.dealTree(res.data);
        });
      },
      dealTree(list) {
        list.forEach(item => {
          if (item.children.length > 0) {
            item.children.forEach(child => {
              if (child.children.length > 0) {
                child.children.forEach(sol => {
                  if (sol.children.length > 0) {

                  } else if (sol.children.length === 0 && sol.resourceList.length > 0) {
                    sol.children = sol.resourceList;
                  }
                })
              } else if (child.children.length === 0 && child.resourceList.length > 0) {
                child.children = child.resourceList;
              }
            });
          }
        })
        return list;
      },
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
        this.getList();
      },
      handleSearchList() {
        this.listQuery.pageNum = 1;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.resource = Object.assign({},defaultResource);
        // this.resource.resourceType = this.defaultCategoryId;
      },
      handleDelete(index, row) {
        this.$confirm('是否要删除该资源?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteResource(row.id).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          });
        });
      },
      handleUpdate(index, row) {
        // console.log(row);
        this.dialogVisible = true;
        this.isEdit = true;
        this.resource = Object.assign({},row);
        if (this.resource.necessaryYn) {
          this.necessaryYn = 1;
        } else {
          this.necessaryYn = 0;
        }
      },
      handleDialogConfirm() {
        if (this.necessaryYn === 1) {
          this.resource.necessaryYn = true;
        } else if (this.necessaryYn === 0) {
          this.resource.necessaryYn = false;
        }
        // console.log(this.resource, this.necessaryYn);
        if (this.isEdit) {
          updateResource(this.resource.id,this.resource).then(response => {
            this.$message({
              message: '修改成功！',
              type: 'success'
            });
            this.dialogVisible =false;
            this.getList();
          })
        } else {
          createResource(this.resource).then(response => {
            this.$message({
              message: '添加成功！',
              type: 'success'
            });
            this.dialogVisible =false;
            this.getList();
          })
        }
      },
      handleShowCategory(){
        this.$router.push({path: '/ums/resourceCategory'})
      },
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        });
      },

    }
  }
</script>
<style></style>


